import {GetWebSocket} from "@/typescript/utils/connection/GetWebSocket";
import {SendTextToWebSocket} from "@/typescript/utils/connection/SendTextToWebSocket";

class WebSocketManager {
    socket: WebSocket;
    suffix: string = '';
    hostname: string = '';
    port: number = 0;
    RestartWebSocket = async () => {
        if (this.socket === undefined || this.socket.readyState !== WebSocket.OPEN) {
            if (this.socket !== undefined) {
                this.socket.close();
            }
            this.socket = await GetWebSocket(this.suffix, this.hostname, this.port);
        }
    }
    StartWebSocket = async (suffix: string, hostname: string, port: number) => {
        console.log('start');
        debugger;
        this.suffix = suffix;
        this.hostname = hostname;
        this.port = port;
        await this.RestartWebSocket();
    }
    SendTextToWebSocket = async (imageContent: string) => {
        return await SendTextToWebSocket(imageContent, this.socket);
    }
}

export {WebSocketManager};